<template>
  <div class="edit-popup">
    <popup
      ref="popupRef"
      :title="popupTitle"
      :async="true"
      width="700px"
      @confirm="handleSubmit"
      @close="handleClose"
    >
      <el-form
        ref="formRef"
        :model="formData"
        label-width="190px"
        :rules="formRules"
      >
        <el-form-item label="等级名称" prop="name" >
          <el-input v-model="formData.name" placeholder="请输入等级名称" />
        </el-form-item>
		<el-form-item label="等级LOGO" prop="type">
		    <material-picker :limit="1" v-model="formData.logo" />
		</el-form-item>
		<el-form-item label="直播间显示LOGO" prop="type">
		    <material-picker :limit="1" v-model="formData.live_logo" />
		</el-form-item>
        <el-form-item label="升级金额" prop="upgrade_amount" >
          <el-input v-model="formData.upgrade_amount" placeholder="请输入升级金额" />
        </el-form-item>
		<el-form-item label="保级金额" prop="grading_amount" >
		  <el-input v-model="formData.grading_amount" placeholder="请输入保级金额" />
		</el-form-item>
		<el-form-item label="多久升级 / 天" prop="days" >
		  <el-input v-model="formData.days" placeholder="请输入多久升级 / 天" />
		</el-form-item>
		<el-form-item label="签到成功赠送鸡蛋数量" prop="balance2" >
		  <el-input v-model="formData.balance2" placeholder="请输入签到成功赠送鸡蛋数量" />
		</el-form-item>
		<el-form-item label="签到成功赠送积分金额" prop="sign_red" >
		  <el-input v-model="formData.balance2" placeholder="请输入签到成功赠送礼品券数量" />
		</el-form-item>
		<el-form-item label="赠送的优惠券ID" prop="coupon_id">
		    <el-select placeholder="无" v-model="formData.coupon_id">
		     <el-option
		            v-for="item in optionsData.category.list"
		            :label="item.name"
		            :value="item.id"
		            :key="item.id"
		        />
		    </el-select>
		</el-form-item>
        <el-form-item label="等级计算开始时间(年月日)" prop="start_time">
           <el-date-picker
                v-model="formData.start_time"
                type="date"
                placeholder="选择日期">
           </el-date-picker>
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from "element-plus";
import {
  levelDetail,
  levelEdit,
  couponList,
  levelAdd
} from "@/api/user";
import Popup from "@/components/popup/index.vue";
import { useDictOptions } from '@/hooks/useDictOptions'
const emit = defineEmits(["success", "close"]);
const formRef = shallowRef<FormInstance>();
const popupRef = shallowRef<InstanceType<typeof Popup>>();
const mode = ref("add");
const popupTitle = computed(() => {
  return mode.value == "edit" ? "编辑等级" : "新增等级";
});
const formData = reactive({
  id:'',
  name: "",
  level: "",
  logo:'',
  live_logo:'',
  upgrade_amount:'',
  grading_amount:'',
  days:'',
  balance2:'',
  sign_red:'',
  coupon_id:'',
  start_time:'',
  level_id:''
});

const formRules = {
  name: [
    {
      required: true,
      message: "请输入等级名称",
      trigger: "blur",
    },
  ],
  level: [
    {
      required: true,
      message: "请输入等级级别",
      trigger: "blur",
    },
  ],
  commission: [
    {
      required: true,
      message: "请输入提成比例",
      trigger: "blur",
    },
  ],
};

const open = (type = "add") => {
  mode.value = type;
  popupRef.value?.open();
};

const { optionsData } = useDictOptions<{
    category: any[]
}>({
    category: {
        api: couponList
    }
})
console.log(optionsData)
const setFormData = (data: Record<any, any>) => {
  for (const key in formData) {
    if (data.info[key] != null && data.info[key] != undefined) {
      //@ts-ignore
      formData[key] = data.info[key];
    }
  }
};

const getDetail = async (row: Record<string, any>) => {
  const data = await levelDetail({
    level_id: row.id,
  });
  setFormData(data);
};

const handleClose = () => {
  emit("close");
};

defineExpose({
  open,
  setFormData,
  getDetail,
});
//提交
const handleSubmit = async () => {
	formData.level_id = formData.id
  await formRef.value?.validate();
  mode.value == "edit"
    ? await levelEdit(formData)
    : await levelAdd(formData);
  popupRef.value?.close();
  emit("success");
};
</script>
